package com.mallcai.bigdata.ladon.service.peekdata.impl;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.Tag;
import com.mallcai.bigdata.ladon.dpl.mapper.peekdata.TagMapper;
import com.mallcai.bigdata.ladon.service.peekdata.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author feixy
 * @date 2019-06-25 15:16
 * @version 1.0
 */
@Service
public class TagService implements ITagService {

    @Autowired
    private TagMapper tagMapper;


    @Transactional
    @Override
    public int add(Tag tag) {
        int count = tagMapper.countByNameOrRule(tag);
        if (count > 0){
            throw new BusinessException(String.format("已经存在标签名称为:%s或匹配值为:%s的记录", tag.getName(), tag.getRule()));
        }
        return tagMapper.insert(tag);
    }

    @Override
    public List<Tag> getByParams(Map<String, Object> params) {
        return tagMapper.getByParams(params);
    }

    @Transactional
    @Override
    public int delete(Tag tag) {
        return tagMapper.delete(tag);
    }

    @Override
    public List<Tag> listAll() {
        return tagMapper.listAll();
    }
}
